package com.bc004346.travelus;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.bc004346.travelus.fragment.StandbyFragment;
import com.bc004346.travelus.model.TravelUsException;
import com.bc004346.travelus.model.abstracts.CustomListActivity;
import com.bc004346.travelus.model.domain.Flight;
import com.bc004346.travelus.model.domain.Passenger;
import com.bc004346.travelus.utility.Utility;

import java.util.ArrayList;

import javax.net.ssl.SSLProtocolException;

import static com.bc004346.travelus.utility.Utility.log;

/**
 * Created by Renats on 10/27/2014.
 * handles retrieving standby list
 */
public class StandbyActivity extends CustomListActivity {

    private StandbyFragment mFragment = new StandbyFragment();
    private Flight mFlight;

    public static final String EXTRA_OPTIONS = "com.bc004346.travelus.StandbyActivity.Options";

    @Override
    protected Fragment getFragment() {
        return mFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTopProgressBar.setVisibility(View.VISIBLE);
        mFlight = (Flight) getIntent().getSerializableExtra(EXTRA_OPTIONS);
        setTitle("Standby List");
        refresh();
    }

    @Override
    public void refresh() {
        mSwipeRefresh.setRefreshing(true);
        mTopProgressBar.setVisibility(View.VISIBLE);

        mStatus.setText("Getting Standby list");
        new AsyncTask<Flight, Void, ArrayList<Passenger>>() {

            private Exception ex = null;

            @Override
            protected ArrayList<Passenger> doInBackground(Flight... flightSearchParams) {
                ArrayList<Passenger> pax = new ArrayList<Passenger>();
                try {
                    pax = Utility.getInstance().getMobileUSAirways().getStandbyList(flightSearchParams[0]);
                } catch (TravelUsException e){
                    ex = e;
                }
                return pax;
            }

            @Override
            protected void onPostExecute(ArrayList<Passenger> strings) {
                mSwipeRefresh.setRefreshing(false);
                mTopProgressBar.setVisibility(View.INVISIBLE);
                if(ex != null && ex.getClass() == SSLProtocolException.class)
                    mStatus.setText("Mobile US Airways is having problems establishing secure connection. Please retry in couple minutes");
                if (ex != null) {
                    mStatus.setText(ex.getMessage());
                    log(ex, StandbyActivity.this);
                } else {
                    mFragment.refresh(strings);
                    mStatus.setText("");

                    if (strings == null || strings.size() == 0)
                        mStatus.setText(getString(R.string.common_no_flights_found));
                }
            }
        }.execute(mFlight);
    }
}
